home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_478 / mp / source / main.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  191 lines

  1. /**************************************************************************
  2. * main.c:    The main program and supporting stuff.
  3. *        Part of MP, the MIDI Playground.
  4. *
  5. * Author:    Daniel Barrett
  6. * Version:    See the file "version.h".
  7. * Copyright:    None!  This program is in the Public Domain.
  8. *        Please share it with others.
  9. ***************************************************************************/
  10.  
  11.     
  12. #include "mp.h"
  13.  
  14. BOOL WorkbenchProgram(char *argv[]);
  15. BOOL CommandLineProgram(int argc, char *argv[]);
  16.     
  17. main(int argc, char *argv[])
  18. {
  19.     BOOL success;
  20.  
  21.     if (argc == 0)
  22.         success = WorkbenchProgram(argv);
  23.     else
  24.         success = CommandLineProgram(argc, argv);
  25.  
  26.     exit(success ? RETURN_OK : RETURN_FAIL);
  27. }
  28.  
  29.     
  30. /************************************************************************
  31. * Initialization of important variables.
  32. ************************************************************************/
  33.  
  34. void InitStuff(FLAGS theFlags[], FILE **in, FILE **out, char **inFile,
  35.         char **outFile)
  36. {
  37.     int i;
  38.  
  39.     for (i=0; i<NUM_FLAGS; i++)    /* Flags. */
  40.         theFlags[i] = (FLAGS)0;
  41.  
  42.     *in      = stdin;        /* Input file. */
  43.     *out     = stdout;        /* Output file. */
  44.     *inFile  = NULL;        /* Input file name. */
  45.     *outFile = NULL;        /* Output file name. */
  46. }
  47.  
  48.     
  49. /************************************************************************
  50. * Open the serial port if necessary, and have fun!
  51. ************************************************************************/
  52.  
  53. BOOL MIDIPlayground(FLAGS theFlags[], FILE *in, FILE *out)
  54. {
  55.     if ((theFlags[FLAG_ITYPE] == OPT_MIDI)
  56.     ||  (theFlags[FLAG_OTYPE] == OPT_MIDI))
  57.     {
  58.         if (!SerialSetup(theFlags[FLAG_SYSEX]))
  59.             return(FALSE);
  60.         ResetSerialPort();
  61.     }
  62.  
  63.     ReadAndWriteStuff(theFlags, in, out);
  64.  
  65.  
  66.     if ((theFlags[FLAG_ITYPE] == OPT_MIDI)
  67.     ||  (theFlags[FLAG_OTYPE] == OPT_MIDI))
  68.         SerialShutdown();
  69.  
  70.     return(TRUE);        /* Stub. */
  71. }
  72.  
  73.     
  74. /************************************************************************
  75. * Use the appropriate I/O functions until we're done.
  76. ************************************************************************/
  77.  
  78. void ReadAndWriteStuff(FLAGS theFlags[], FILE *in, FILE *out)
  79. {
  80.     MIDI_RESULT    result;
  81.     MIDI_VALUE    value;
  82.     MIDI_RESULT    (*getfcn)(FILE *f, MIDI_VALUE *value);
  83.     BOOL        (*putfcn)(FILE *f, MIDI_VALUE value);
  84.     void        (*skipfcn)(FILE *f, MIDI_VALUE value);
  85.  
  86.     SetTheFunctions(&getfcn, &putfcn, &skipfcn, theFlags);
  87.     
  88.     while ((result = getfcn(in, &value)) != RESULT_STOP)
  89.     {
  90.         switch (result)
  91.         {
  92.             case RESULT_OK:
  93.                 putfcn(out, value);
  94.                 break;
  95.             case RESULT_ERROR:
  96.                 fprintf(out, "ERROR\n");
  97.                 skipfcn(in, value);
  98.                 break;
  99.             case RESULT_OVERFLOW:
  100.                 fprintf(out, "VALUE TOO BIG (overflow)\n");
  101.                 skipfcn(in, value);
  102.                 break;
  103.             default:
  104.                 skipfcn(in, value);
  105.                 fprintf(out, "An unknown error occurred.\n");
  106.                 break;
  107.         }
  108.     }
  109. }
  110.  
  111.  
  112. /************************************************************************
  113. * Choose the appropriate I/O functions.
  114. ************************************************************************/
  115.  
  116. void SetTheFunctions(MIDI_RESULT (**getfcn)(), BOOL (**putfcn)(),
  117.             void (**skipfcn)(), FLAGS theFlags[])
  118. {
  119.     switch (theFlags[FLAG_ITYPE])
  120.     {
  121.         case OPT_TEXT:
  122.             *getfcn = FromText;
  123.             *skipfcn = SkipText;
  124.             break;
  125.         case OPT_BINARY:
  126.             *getfcn = FromBinary;
  127.             *skipfcn = SkipBinary;
  128.             break;
  129.         case OPT_MIDI:
  130.             *getfcn = FromMidi;
  131.             *skipfcn = SkipMidi;
  132.             break;
  133.         default:
  134.             *getfcn = FromText;    /* Least dangerous fcn. */
  135.             *skipfcn = SkipText;    /* Least dangerous fcn. */
  136.     }
  137.  
  138.     switch (theFlags[FLAG_OTYPE])
  139.     {
  140.         case OPT_TEXT:
  141.             *putfcn = ToText;
  142.             break;
  143.         case OPT_BINARY:
  144.             *putfcn = ToBinary;
  145.             break;
  146.         case OPT_MIDI:
  147.             *putfcn = ToMidi;
  148.             break;
  149.         default:
  150.             *putfcn = ToText;    /* Least dangerous fcn. */
  151.             break;
  152.     }
  153. }
  154.  
  155.  
  156. /* Is "opt" a valid input/output type?  Set the flag too. */
  157.     
  158. BOOL CheckIOType(char opt, int ioFlag, FLAGS theFlags[])
  159. {
  160.     return(IsIOType(theFlags[ioFlag] = opt));
  161. }
  162.  
  163.  
  164. /* Is "ioType" a valid input/output type? */
  165.     
  166. BOOL IsIOType(char ioType)
  167. {
  168.     return(    (ioType == OPT_TEXT)
  169.     ||    (ioType == OPT_BINARY)
  170.     ||    (ioType == OPT_MIDI));
  171. }
  172.  
  173.  
  174. /* Did user specify both input and output types? */
  175.     
  176. BOOL CheckFlags(FLAGS theFlags[])
  177. {
  178.     return(theFlags[FLAG_ITYPE] && theFlags[FLAG_OTYPE]);
  179. }
  180.  
  181.     
  182. /************************************************************************
  183. * ^C handling -- Manx stuff.
  184. ************************************************************************/
  185.  
  186. void _abort(void)
  187. {
  188.     SerialShutdown();
  189.     exit(RETURN_FAIL);
  190. }
  191.